﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.BL;
using VINETLIFE.DO;
using System.Data;

namespace VINETLIFE.AdminCP
{
    public partial class Search : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            tlCategory.DataSource = new cmsProductCategoryBL().SelectAll();
            tlCategory.DataBind();
            tlCategory.ExpandToLevel(3);
            //  if(!IsPostBack)
            //    grvProductDataSource(0);
        }
        //protected void grvProductDataSource(int productCategoryID)
        //{
        //    grvProduct.DataSource = new cmsProductBL().SelectByProductCategory(productCategoryID);
        //    grvProduct.DataBind();
        //}
        protected void grvProductDataSource(int productCategoryID, string keyword)
        {
            grvProduct.DataSource = new cmsProductBL().SelectByProductCategoryAndKeyword(productCategoryID, keyword);
            grvProduct.DataBind();
        }
        //protected void ddlParentCate_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    int productCategoryID = int.Parse(ddlParentCate.SelectedValue);
        //    grvProductDataSource(productCategoryID);
        //}
        public DataTable ProductPropertiesDataSource(int productID)
        {
            return new cmsProductPropertyBL().SelectByProductID(productID);
        }
        protected void grvProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ArticleID = int.Parse(grvProduct.DataKeys[grvProduct.SelectedIndex].Value.ToString());
            Response.Redirect("Product.aspx?ProductID=" + ArticleID.ToString());
        }
        protected void grvProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productID = Convert.ToInt32(grvProduct.DataKeys[e.RowIndex].Value);
            new cmsProductBL().Delete(new cmsProductDO { ProductID = productID });
            new cmsProductPropertyBL().DeleteByProductID(productID);
            Functions.Alert("Xóa bản tin thành công!", Request.Url.ToString());
        }
        protected void grvProduct_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //grvProduct.PageIndex = e.NewPageIndex;
            //if (ddlParentCate.SelectedValue.Equals("0"))
            //    grvProductDataSource(0);
            //else
            //    grvProductDataSource(int.Parse(ddlParentCate.SelectedValue));
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int categoryID =int.Parse(tlCategory.FocusedNode.GetValue("ProductCategoryID").ToString());
            string keyword = txtKeyWord.Text.Trim();
            grvProductDataSource(categoryID, keyword);
        }
    }
}